package com.example.edu.controller.frontController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.edu.entity.Course;
import com.example.edu.entity.Teacher;
import com.example.edu.service.CourseService;
import com.example.edu.service.TeacherService;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/edu")
@CrossOrigin
public class TeacherFrontController {
    
    @Autowired
    TeacherService teacherService;

    @Autowired
    CourseService courseService;

    @GetMapping("getFrontTeacher/{teacherId}")
    public Result getTeacherInfo(@PathVariable String teacherId){

        Teacher teacher = teacherService.getById(teacherId);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<Course>();
        queryWrapper.eq("teacher_id",teacherId);
        List<Course> courseList = courseService.list(queryWrapper);

        return Result.ok().data("teacher",teacher).data("courseList",courseList);
    }
    
    @GetMapping("/getFrontTeacher/{page}/{limit}")
    public Result getFrontTeacher(@PathVariable Long page,@PathVariable Long limit){
        Page<Teacher> teacherPage = new Page<>(page,limit);

        teacherService.page(teacherPage,null);
        List<Teacher> teacherList = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        long current = teacherPage.getCurrent();
        long size = teacherPage.getSize();
        long pages = teacherPage.getPages();
        boolean isNext = teacherPage.hasNext();
        boolean isPrevious = teacherPage.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("current", current);
        map.put("size", size);
        map.put("total",total);
        map.put("teacherList", teacherList);
        map.put("pages",pages);
        map.put("isNext",isNext);
        map.put("isPrevious", isPrevious);

        return Result.ok().data("map",map);

    }
}
